package com.she.iiicoursecenter.tools;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.telephony.TelephonyManager;

public class Utils {
	private Context mContext;

	ConnectivityManager mConnectivityManager;
	TelephonyManager 	mTelephonyMgr;

	public Utils(Context context) {
		// TODO Auto-generated constructor stub
		mContext	= context;
		mConnectivityManager 	= (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		mTelephonyMgr 			= (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
	}

	public boolean isNetworkAvailable() {
		boolean isAvailable = false;
		if(mConnectivityManager != null) {
			NetworkInfo[] info = mConnectivityManager.getAllNetworkInfo();
			if(info != null) {
				for(int i = 0; i < info.length; i++) {
					if(info[i].getState() == NetworkInfo.State.CONNECTED) {
						isAvailable = true;
						break;
					}
				}
			}
		}
		return isAvailable;
	}

	public String getIMSI(){
		String imsi = mTelephonyMgr.getSubscriberId();
		return imsi;
	}

	public String getIMEI(){
		String imei = mTelephonyMgr.getDeviceId();
		return imei;
	}
	// translate dip to pixel unit...
	public int dip2px(float dpValue) {

		final float scale = mContext.getResources().getDisplayMetrics().density;  
		return (int) (dpValue * scale + 0.5f);  
	}  
	// translate pixel to dip unit...
	public int px2dip(float pxValue) {  

		final float scale = mContext.getResources().getDisplayMetrics().density;  
		return (int) (pxValue / scale + 0.5f);  
	}
	//
	public int getScreenWidth(){

		final int width = mContext.getResources().getDisplayMetrics().widthPixels;
		return width;
	}
	//
	public int getScreenHeight(){

		final int height = mContext.getResources().getDisplayMetrics().heightPixels;
		return height;
	}

	public static int getRandom(int start, int end) {
		return (int) (Math.random() * (end - start + 1)) + start;
	} 

	public Bitmap getBitmapFromResource(int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
		opt.inPurgeable 		= true;
		opt.inInputShareable 	= true;
		opt.inJustDecodeBounds 	= true;
		try {
			BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(opt,true);
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (SecurityException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (NoSuchFieldException e1) {
			e1.printStackTrace();
		}

		InputStream is = mContext.getResources().openRawResource(resId);
		BitmapFactory.decodeStream(is, null, opt);
		opt.inJustDecodeBounds = false; 
		//		opt.inSampleSize = 2;
		return BitmapFactory.decodeStream(is, null, opt);
	}

	/*
	 * 
	 *      These functions will not be used in this project....
	 * 
	 * 
	public void sendSMS(Activity a, String message){
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		sendIntent.putExtra("sms_body", message); 
		sendIntent.setType("vnd.android-dir/mms-sms");
		sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		a.startActivity(sendIntent);
	}

	public void enumEmailAccount(List<String> Emails){
		if (Emails == null) return;

		Emails.clear();
		Account[] accounts = AccountManager.get(mContext).getAccounts();
		for (Account account : accounts) {
			Iterator<String> it = Emails.iterator();
			boolean find = false;
			while(it.hasNext()){
				if (it.next().equals(account.name)){
					find = true;
					break;
				}
			}
			if (find==false && account.name.contains("@")){ // remove duplication and illegal address...
				Emails.add(account.name);
				//Log.d("enumEmailAccount", "Email = " + account.name);
			}    		
		}
	}

	public void sendEmail(Activity a, String subject, String content, String ChooserTitle){

		// these fields will not be used...just memo...
		//    	String aEmailList[] = { "user@fakehost.com","user2@fakehost.com" };  
		//    	String aEmailCCList[] = { "user3@fakehost.com","user4@fakehost.com"};  
		//    	String aEmailBCCList[] = { "user5@fakehost.com" };      	  
		//    	emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);  
		//    	emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);  
		//    	emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
		//    	emailIntent.setType("application/octet-stream");
		//    	emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));  // Attachment...
		//    	emailIntent.setType("text/html");
		//    	emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<b>this is html text in email body.</b>"));

		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		emailIntent.setType("plain/text");
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
		emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		a.startActivity(Intent.createChooser(emailIntent, ChooserTitle));

	}

	public void postTextOnGoolePlus(Activity a, String message){ // post text message on Google+ wall...

		Intent shareIntent = ShareCompat.IntentBuilder.from(a)
				.setText(message)
				.setType("text/plain")
				.getIntent()
				.setPackage("com.google.android.apps.plus")
				.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		a.startActivity(shareIntent);
	}

	public void postMediaOnGooglePlus(Activity a, String message, Uri MEDIA_URI, String MEDIA_TYPE){ // post media(like image/png or video/mp4...) on Google+ wall...
		Intent shareIntent = ShareCompat.IntentBuilder.from(a)
				.setText(message)
				.setStream(MEDIA_URI)
				.setType(MEDIA_TYPE)
				.getIntent()
				.setPackage("com.google.android.apps.plus")
				.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		a.startActivity(shareIntent);
	}
	
	public void sendTweet(Activity a, String message){ // post text message on Twitter wall...
		
        String tweetUrl = "https://twitter.com/intent/tweet?text=" + message;
        Uri uri = Uri.parse(tweetUrl);
        a.startActivity(new Intent(Intent.ACTION_VIEW, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
	}
	*/
}
